package com.wang.myblog.service.impl;

import com.wang.myblog.controller.common.UploadController;
import com.wang.myblog.entity.Link;
import com.wang.myblog.mapper.LinkMapper;
import com.wang.myblog.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LinkServiceImpl implements LinkService {

    @Autowired
    private LinkMapper linkMapper;

    @Override
    public boolean insert(Link link) {
        if(linkMapper.insert(link)==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean deleteById(Integer id) {
        Link link=linkMapper.queryById(id);
        if(linkMapper.deleteById(id)==1){
            UploadController.deleteCommonImg(link.getImg());
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Link queryById(Integer id) {
        return linkMapper.queryById(id);
    }

    @Override
    public List<Link> queryAll() {
        return linkMapper.queryAll();
    }

    @Override
    public boolean update(Link link) {
        Link link1=linkMapper.queryById(link.getId());
        if(linkMapper.update(link)==1){
            if(!link.getImg().equals(link1.getImg())) {
                UploadController.deleteCommonImg(link1.getImg());
            }
            return true;
        }else {
            return false;
        }
    }
}
